#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
#endregion

namespace TankWars
{
    public sealed partial class FPS : Microsoft.Xna.Framework.DrawableGameComponent
    {
        #region Member Variables
        private float fps;
        private float updateInterval = 1.0f;
        private float timeSinceLastUpdate = 0.0f;
        private float framecount = 0;
        // Mouse variable for coordinates
        MouseState myMouseState;
        #endregion

        public FPS(Game game)
            : this(game, false, false, game.TargetElapsedTime)
        {
            // Set up mouse state
            myMouseState = new MouseState();
        }

        public FPS(Game game, bool synchWithVerticalRetrace, bool isFixedTimeStep, TimeSpan targetElapsedTime)
            : base(game)
        {
            GraphicsDeviceManager graphics = (GraphicsDeviceManager)Game.Services.GetService(typeof(IGraphicsDeviceManager));

            graphics.SynchronizeWithVerticalRetrace = synchWithVerticalRetrace;
            Game.IsFixedTimeStep = isFixedTimeStep;
            Game.TargetElapsedTime = targetElapsedTime;
        }

        /// <summary>
        /// Allows the game component to perform any initialization that it 
        ///  needs to perform before starting to run.  This is where it can query for
        ///  any required services and load content.
        /// </summary>
        public sealed override void Initialize()
        {
            // TODO: Initialize code here
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself
        /// </summary>
        public sealed override void Update(GameTime gameTime)
        {
            // Continually calculate new mouse coordinates
            myMouseState = Mouse.GetState();

            base.Update(gameTime);
        }

        /// <summary>
        /// Draw routine
        /// </summary>
        public sealed override void Draw(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            framecount++;
            timeSinceLastUpdate += elapsed;
            if (timeSinceLastUpdate > updateInterval)
            {
                fps = framecount / timeSinceLastUpdate;

#if XBOX
                System.Diagnostics.Debug.WriteLine("FPS: " + fps.ToString());
#else
                Game.Window.Title = "FPS: " + fps.ToString() + "   Mouse Coords: " + myMouseState.X + " X, " + myMouseState.Y + " Y";
#endif
                framecount = 0;
                timeSinceLastUpdate -= updateInterval;
            }
            base.Draw(gameTime);
        }
    }
}
